#!/local/bin/perl
# PERL subroutine:
# Tell whether something is a legal E-mail address or not

sub isemail {
    my ($addr, $restrict) = @_;
    # just to get over -w..
    $restrict = '' if !defined($restrict);
    if ($addr !~ /(\S+)@(\S+)/) {
        $notemailbecause = "No @ sign found in address";
        return 0;
    } else {
        $localpart = $1;
        $domain = $2;
    }
    if ($domain !~ /bitnet|uucp$/i && $restrict ne "nodns") {
        $ckroute = `/usr/bin/host -t mx $domain 2>&1; /usr/bin/host -t a $domain 2>&1`;
        if ($ckroute !~ /is handled |has address/) {
	    $notemailbecause = "Name lookup failure: $ckroute";
            return 0;
	} elsif ($ckroute =~ /has address 127.0.0.1/) {
	    $notemailbecause = "Points at loopback interface";
	    return 0;
	}
    } 
    if ($localpart =~ /^".*"$/) {
        # Quoted-string; no further checks (could check for unbalance..)
    } elsif ($localpart =~ /[ ()<>@,;:\\"\[\]]/) { # "
	$notemailbecause = "Illegal character in local part: $&";
	return 0;
    }

    return 1;
}

1;
